//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.yaukie.base.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaukie.base.dto.BaseData;

public class DatabaseUtil {
    private static final Logger log = LoggerFactory.getLogger(DatabaseUtil.class);
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/encrypt?useUnicode=true&characterEncoding=utf8";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";
    private static final String SQL = "SELECT * FROM ";
    private static ThreadLocal<Connection> hold = new ThreadLocal();

    public DatabaseUtil() {
    }

    public static Connection getConnection() {
        Connection conn = null;

        try {
            if (conn == null) {
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/encrypt?useUnicode=true&characterEncoding=utf8", "root", "root");
                hold.set(conn);
            } else {
                conn = (Connection)hold.get();
            }
        } catch (SQLException var2) {
            log.error("get connection failure", var2);
        }

        return conn;
    }

    public static void closeConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException var2) {
                log.error("close connection failure", var2);
            }
        }

    }

    public static List<String> getColumnNames(String tableName) {
        List<String> columnNames = new ArrayList();
        Connection conn = getConnection();
        PreparedStatement pStemt = null;
        String tableSql = "SELECT * FROM " + tableName;

        try {
            pStemt = conn.prepareStatement(tableSql);
            ResultSetMetaData rsmd = pStemt.getMetaData();
            int size = rsmd.getColumnCount();

            for(int i = 0; i < size; ++i) {
                columnNames.add(rsmd.getColumnName(i + 1));
            }
        } catch (SQLException var16) {
            log.error("getColumnNames failure", var16);
        } finally {
            if (pStemt != null) {
                try {
                    pStemt.close();
                    closeConnection(conn);
                } catch (SQLException var15) {
                    log.error("getColumnNames close pstem and connection failure", var15);
                }
            }

        }

        return columnNames;
    }

    public static List<String> getColumnTypes(String tableName) {
        List<String> columnTypes = new ArrayList();
        Connection conn = getConnection();
        PreparedStatement pStemt = null;
        String tableSql = "SELECT * FROM " + tableName;

        try {
            pStemt = conn.prepareStatement(tableSql);
            ResultSetMetaData rsmd = pStemt.getMetaData();
            int size = rsmd.getColumnCount();

            for(int i = 0; i < size; ++i) {
                columnTypes.add(rsmd.getColumnTypeName(i + 1));
            }
        } catch (SQLException var16) {
            log.error("getColumnTypes failure", var16);
        } finally {
            if (pStemt != null) {
                try {
                    pStemt.close();
                    closeConnection(conn);
                } catch (SQLException var15) {
                    log.error("getColumnTypes close pstem and connection failure", var15);
                }
            }

        }

        return columnTypes;
    }

    public static List<String> getColumnComments(String tableName) {
        new ArrayList();
        Connection conn = getConnection();
        PreparedStatement pStemt = null;
        String tableSql = "SELECT * FROM " + tableName;
        List<String> columnComments = new ArrayList();
        ResultSet rs = null;

        try {
            pStemt = conn.prepareStatement(tableSql);
            rs = pStemt.executeQuery("show full columns from " + tableName);

            while(rs.next()) {
                columnComments.add(rs.getString("Comment"));
            }
        } catch (SQLException var16) {
            var16.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                    closeConnection(conn);
                } catch (SQLException var15) {
                    log.error("getColumnComments close ResultSet and connection failure", var15);
                }
            }

        }

        return columnComments;
    }

    public static List<BaseData> getBaseDataList(String tableName) {
        List<String> columnNames = getColumnNames(tableName);
        List<String> columnTypes = getColumnTypes(tableName);
        List<String> columnComments = getColumnComments(tableName);
        List<BaseData> list = new ArrayList();

        for(int i = 0; i < columnNames.size(); ++i) {
            BaseData baseData = new BaseData();
            baseData.setColumnName(firstUpperCamelCase((String)columnNames.get(i)));
            String columnType = null;
            switch ((String)columnTypes.get(i)) {
                case "VARCHAR":
                    columnType = "String";
                    break;
                case "DATETIME":
                    columnType = "Date";
                    break;
                case "INT":
                    columnType = "Integer";
                    break;
                case "TINYINT":
                    columnType = "Integer";
                    break;
                case "DECIMAL":
                    columnType = "BigDecimal";
                    break;
                case "TEXT":
                    columnType = "String";
                    break;
                case "TIMESTAMP":
                    columnType = "timestamp";
                    break;
                default:
                    columnType = "未知类型";
                    System.out.println("存在不支持类型！请手写。");
            }

            baseData.setColumnType(columnType);
            baseData.setColumnComment((String)columnComments.get(i));
            list.add(baseData);
        }

        return list;
    }

    public static String firstUpperCamelCase(String str) {
        if (!StringUtils.isNotBlank(str)) {
            return str;
        } else {
            str = str.replace("T_", "");
            str = str.toLowerCase();
            String[] strs = str.split("_");
            if (strs.length == 1) {
                return str.substring(0, 1).toLowerCase() + str.substring(1);
            } else {
                String convertedStr = "";

                for(int i = 0; i < strs.length; ++i) {
                    convertedStr = convertedStr + firstLetterUpper(strs[i]);
                }

                return convertedStr.substring(0, 1).toLowerCase() + convertedStr.substring(1);
            }
        }
    }

    public static String firstLetterUpper(String str) {
        if (StringUtils.isNotBlank(str)) {
            str = str.replace("T_", "");
            str = str.toLowerCase();
            return str.substring(0, 1).toUpperCase() + str.substring(1, str.length());
        } else {
            return str;
        }
    }

    public static void main(String[] args) {
        List<BaseData> table = getBaseDataList("t_user");
        Iterator var2 = table.iterator();

        while(var2.hasNext()) {
            BaseData baseData = (BaseData)var2.next();
            log.info(baseData.getColumnName());
        }

    }
}
